<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
      class="white-normal-box">
      <el-form-item label="用户名称" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名称" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="下单时间">
        <el-date-picker
          v-model="rangeTime"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="white-normal-box">
      <el-row :gutter="10" class="mb20">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="teamList" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键id" align="center" prop="id" /> -->
        <el-table-column label="用户名称" align="center" prop="userName" />
        <el-table-column label="推荐人名称" align="center" prop="superiorName">
          <template slot-scope="{ row }">
            <div>
              {{ row.superiorName || '-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="订单金额" align="center" prop="orderPrice" />
        <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
          <template slot-scope="{ row }">
            {{ parseTime(row.orderTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-download" size="mini"
              @click="handleUpdate(scope.row)">导出</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>

    <!-- 添加或修改金币等级对话框 -->
    <el-dialog title="导出流水记录" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="流水时段">
          <el-date-picker
            v-model="timeRange"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
          <el-tooltip class="item" effect="dark" content="不选默认今日" placement="top">
            <i class="el-icon-question" style="margin-left: 10px; font-size: 16px; cursor: pointer"></i>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleExport">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNewUser } from "@/api/system/users";
export default {
  name: "newUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 金币等级表格数据
      teamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        startTime: undefined,
        endTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      timeRange: [],
      rangeTime: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询金币等级列表 */
    getList() {
      if (this.rangeTime && this.rangeTime.length == 2) {
        this.queryParams.startTime = this.rangeTime[0] + " 00:00:00"
        this.queryParams.endTime = this.rangeTime[1] + " 23:59:59"
      } else {
        this.queryParams.startTime = undefined
        this.queryParams.endTime = undefined
      }
      this.loading = true;
      listNewUser(this.queryParams).then(response => {
        this.teamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.timeRange = []
      this.form = {
        startTime: undefined,
        endTime: undefined,
        userId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.rangeTime = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.userId = row.userId
      this.open = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.timeRange && this.timeRange.length == 2) {
        this.form.startTime = this.timeRange[0] + 'T00:00:00'
        this.form.endTime = this.timeRange[1] + 'T23:59:59'
      } else {
        let date = new Date()
        let year = date.getFullYear()
        let month = date.getMonth() + 1
        let day = date.getDate()
        let str = `${year}-${month < 10 ? 0 : ''}${month}-${day < 10 ? 0 : ''}${day}`
        this.form.startTime = str + 'T00:00:00'
        this.form.endTime = str + 'T23:59:59'
      }
      this.download('system/user/userListExport', {
        ...this.form
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
